﻿#region Disclaimer / License
// Copyright (C) 2011, Kenneth Bergeron, IAP Worldwide Services, Inc
// NOAA :: National Marine Fisheries Service
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
#endregion
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;

namespace DataSets
{
    public partial class EASASettingsDataSet
    {
        public static string DefaultXMLFile
        {
            get
            {
                return System.IO.Path.Combine(
                    Application.StartupPath, System.IO.Path.Combine("StaticData", "EASASettings.XML"));
            }
        }
        public Exception Load()
        {
            return Load(DefaultXMLFile);
        }
        public Exception Load(string aFile)
        {
            if (!System.IO.File.Exists(aFile)) return new Exception("File " + aFile + ": Not found.");
            Exception Result = null;
            try
            {
                this.Clear();
                this.ReadXml(aFile);
            }
            catch (Exception Ex)
            {
                Result = Ex;
                Debug.WriteLine(Ex);
            }
            return Result;
        }
        public Exception Save()
        {
            return Save(DefaultXMLFile);
        }
        public Exception Save(string aFile)
        {
            Exception Result = null;
            try
            {
                this.WriteXml(aFile);
            }
            catch (Exception Ex)
            {
                Result = Ex;
                Debug.WriteLine(Ex);
            }
            return Result;
        }
        public string VesselCode(string aVesselName)
        {
            return (from VesselCodesRow qR in this.VesselCodes
                    where qR.DESCRIPTION == aVesselName
                    select qR.CODE)
                    .DefaultIfEmpty(aVesselName)
                    .First();
        }
        public string VesselName(string aCode)
        {
            VesselCodesRow Row = this.VesselCodes.FindByCODEisactive(aCode, true);
            if (Row == null) return aCode;
            return Row.DESCRIPTION;
        }
        public string MeasurmentName(string aCode)
        {
            MeasurementGeneralCodesRow Row = this.MeasurementGeneralCodes.FindByCODEisactive(aCode, true);
            if (Row == null) return aCode;
            return Row.DESCRIPTION;
        }
        public string SampleName(string aCode)
        {
            LengthSampleCodesRow Row = this.LengthSampleCodes.FindByCODEisactive(aCode, true);
            if (Row == null) return aCode;
            return Row.DESCRIPTION;
        }
        public string TagName(string aCode)
        {
            TagCodesRow Row = this.TagCodes.FindByCODEisactive(aCode, true);
            if (Row == null) return aCode;
            return Row.DESCRIPTION;
        }
        public string Describe(string aSample, string aCode)
        {
            string Result = "Unknown CODE for Sample: " + aSample + ": " + aCode;
            if (aSample.Equals("Sample", StringComparison.InvariantCultureIgnoreCase))
            {
                string D = this.SampleName(aCode);
                if (!string.IsNullOrEmpty(D)) Result = D;
            }
            if (aSample.Equals("Length", StringComparison.InvariantCultureIgnoreCase))
            {
                string D = this.MeasurmentName(aCode);
                if (!string.IsNullOrEmpty(D)) Result = D;
            }
            if (aSample.Equals("Tag", StringComparison.InvariantCultureIgnoreCase))
            {
                string D = this.TagName(aCode);
                if (!string.IsNullOrEmpty(D)) Result = D;
            }
            return Result;
        }
    }
}